<template>
  <basic-container>
    <avue-crud
      :option="option"
      :search.sync="search"
      :table-loading="loading"
      :data="data"
      :page.sync="page"
      :permission="permissionList"
      :before-open="beforeOpen"
      v-model="form"
      ref="crud"
      @row-update="rowUpdate"
      @row-save="rowSave"
      @row-del="rowDel"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @sort-change="sortChange"
      @on-load="onLoad"
    >
      <template slot="menuLeft">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="small"
          @click.stop="handleSave"
          >新 建</el-button>
      </template>
      <template slot-scope="{ row }" slot="isOn">
        <el-switch
          :active-value="1"
          :inactive-value="0"
          v-model="row.isOn"
          @change="handleSwitch(row)"
        >
        </el-switch>
      </template>
      <!-- 总交易笔数 -->
      <template slot-scope="{ row }" slot="rsSn">
        <div
          :class="row.rpmNo == 'R00001'||row.rptNo == '32' ? 'rsSnblue' : ' '"
          @click.stop="getqrCodeUrl(row)"
        >
          {{ row.rsSn }}
        </div>
      </template>

      <template slot-scope="scope" slot="menu">
        <el-button
          icon="el-icon-key"
          size="small"
          type="text"
          v-if="permissionList.delBtn && scope.row.rptNo !='32'"
          @click.stop="rowDel(scope.row)"
          >解 绑
        </el-button>
        <el-button
          size="small"
          type="text"
          v-if="permissionList.delBtn"
          @click.stop="goto(scope.row)"
          >终端管理
        </el-button>
      </template>
      <template slot-scope="{}" slot="rpiNoSearch">
        <select-page
          ref="searchRpiSelect"
          label="rpiName"
          value="rpiNo"
          :select.sync="search.rpiNo"
          :url="website.searchLink.merchantUrl"
          placeholder-text="商户"
          filter-key="searchParam"
          @update:select="searchrpiNoChange"
          @clear:select="searchrpiNoClear"
        ></select-page>
      </template>
      <template slot-scope="{}" slot="storeNoSearch">
        <select-page
          ref="searchRpsSelect"
          label="rpsName"
          value="rpsNo"
          :select.sync="search.storeNo"
          :url="website.searchLink.rpsUrl"
          placeholder-text="门店"
          filter-key="searchParam"
          :params="{
            rpiNo: search.rpiNo
          }"
          @update:select="searchrpsNoChange"
          @clear:select="searchrpsNoClear"
        ></select-page>
      </template>
      <template slot-scope="{}" slot="rpstNoSearch">
        <select-page
          ref="searchRpstSelect"
          label="rpstName"
          value="rpstNo"
          :select.sync="search.rpstNo"
          :url="website.searchLink.rpstUrl"
          placeholder-text="收银员"
          filter-key="searchParam"
          :params="{
            rpiNo: search.rpiNo,
            rpsNo: search.storeNo,
          }"
        ></select-page>
      </template>
    </avue-crud>
    <el-dialog
      title="数据导入"
      append-to-body
      :visible.sync="excelBox"
      width="555px"
    >
      <avue-form
        :option="excelOption"
        v-model="excelForm"
        :upload-after="uploadAfter"
      >
        <template slot="excelTemplate">
          <el-button type="primary" @click="handleTemplate">
            点击下载<i class="el-icon-download el-icon--right"></i>
          </el-button>
        </template>
      </avue-form>
    </el-dialog>
    <!-- 新建 -->
    <el-dialog
      :title="terminalDialog.text"
      append-to-body
      :close-on-click-modal="false"
      :visible.sync="terminalDialog.visible"
      v-if="terminalDialog.visible"
      :before-close="terminalDialogHandleClose"
      ref="terDialog"
    >
      <terminalDialog
        ref="terminalDialog"
        :terminalDialog="terminalDialog"
        :rptNoList="rptNoList"
        :serviceNoList="serviceNoList"
        :terminalDForm="terminalDForm"
      />
      <span slot="footer" class="dialog-footer">
        <el-button
          @click="
            () => {
              terminalDialog.visible = false;
              this.$refs['terminalDialog'].initEmpty();
            }
          "
          >取 消</el-button
        >
        <el-button
          type="primary"
          @click="terminalDialogSubmit"
          v-if="!terminalDialog.disabled"
          >确 定</el-button
        >
      </span>
    </el-dialog>
    <el-image-viewer
      style="z-index: 2048"
      v-if="payVoucherDialog"
      :on-close="
        () => {
          payVoucherDialog = false;
        }
      "
      :url-list="[rqUrlImage]"
    ></el-image-viewer>
    <!-- 二级密码 -->
    <el-dialog
      title="二级密码"
      append-to-body
      width="440px"
      :close-on-click-modal="false"
      v-if="twoPwdShow"
      :visible.sync="twoPwdShow"
      :before-close="handleCloseTwoPwd"
    >
      <el-form
        :model="towPwdForm"
        ref="towPwdForm"
        label-width="0"
        class="demo-ruleForm"
      >
        <el-form-item
          label=""
          prop="towPwd"
          :rules="[
            {
              required: true,
              message: '二级密码不能为空',
              trigger: ['blur', 'change'],
            },
            {
              pattern: /^[0-9]{6}$/,
              message: '二级密码由6位数字组成',
              trigger: ['blur', 'change'],
            },
          ]"
        >
          <el-input
            show-password
            v-model.number="towPwdForm.towPwd"
            autocomplete="off"
            placeholder="输入二级密码"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="twoPwdSubmit()">确 定</el-button>
      </div>
    </el-dialog>
  </basic-container>
</template>

<script>
import {
  getList,
  getDetail,
  add,
  update,
  remove,
  incomingTerminalOn,
  getRptNo,
  getModelList,
  getrpiNo,
  getstoreNo,
  getrpstNo,
  removeSM,
  unBindYSPOS,
  unBindZNJ,
} from "@/api/rb-oem/terminal/terminal";
import { mapGetters } from "vuex";
import { getToken } from "@/util/auth";
import terminalDialog from "./terminalDialog";
import { userHasInfo } from "@/api/rb-oem/forgetPassword/forgetPassword";
import md5 from "js-md5";
import { remove as terRemove } from "@/api/rb-oem/posinfo/terminal/taika";
import selectPage from "@/components/select-page/select-page";

export default {
  components: {
    selectPage,
    terminalDialog,
  },
  data() {
    return {
      payVoucherDialog: false,
      rqUrlImage: [],
      form: {},
      query: {},
      search: {},
      loading: true,
      excelBox: false,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      selectionList: [],
      option: {
        height: "calc(50vh - 50px)",
        calcHeight: 30,
        tip: false,
        border: true,
        index: true,
        selection: true,
        searchMenuSpan: 6,
        columnBtn: false,
        addBtn: false, //取消表格 新建/添加
        editBtn: false,
        viewBtn: false,
        delBtn: false,
        viewTitle: "修 改",
        labelWidth: 100,
        menuWidth: 150,
        column: [
          {
            label: "机构编号",
            prop: "serNo",
            type: "input",
            display: false,
            minWidth: 140,
            overHidden: true,
          },
          {
            label: "机构名称",
            prop: "serCompany",
            type: "input",
            display: false,
            overHidden: true,
            minWidth: 180,
          },
          {
            label: "直属代理编号",
            prop: "serviceNo",
            type: "input",
            minWidth: 140,
            display: false,
            overHidden: true,
          },
          {
            label: "直属代理名称",
            prop: "serviceName",
            type: "input",
            minWidth: 180,
            display: false,
            overHidden: true,
          },
          {
            label: "终端简称",
            prop: "rtName",
            type: "input",
            display: false,
            overHidden: true,
            minWidth: 200,
          },
          {
            label: "终端编号",
            prop: "rsSn",
            type: "input",
            search: true,
            slot: true,
            rules: [
              {
                required: true,
                message: "请输入终端编号",
                trigger: "blur",
              },
            ],
            minWidth: 200,
            overHidden: true,
          },
          {
            label: "门头名称",
            prop: "rpiName",
            type: "input",
            display: false,
            width: 200,
          },
          {
            label: "商户编号",
            searchLabel: "商户",
            prop: "rpiNo",
            type: "select",
            formslot: true,
            filterable: true,
            search: true,
            hide: true,
            searchOrder: 4,
            display: false,
            rules: [
              {
                required: true,
                message: "请输入商户",
                trigger: "blur",
              },
            ],
            // props: {
            //   label: 'rpiName',
            //   value: 'rpiNo',
            //   desc:'rpiNo'
            // },
            // // remote: true,
            // // dicFormatter:(data)=>{
            // //   return data.data.records
            // // },
            // change: (value) => {
            //   if (value) {
            //     this.search.storeNo = '';
            //     this.storeNoList = [];
            //     this.search.rpstNo = '';
            //     this.rpstNoList = [];
            //     // if (value.value) {
            //     //   this.rpiNoListChange(value.value);//门店
            //     // }
            //   }
            // },
            overHidden: true,
          },
          {
            label: "门店名称",
            prop: "storeName",
            type: "input",
            display: false,
            overHidden: true,
            minWidth: 180,
          },
          {
            label: "门店编号",
            searchLabel: "门店",
            prop: "storeNo",
            type: "select",
            sortable: true,
            filterable: true,
            search: true,
            searchOrder: 5,
            hide: true,
            display: false,
            rules: [
              {
                required: true,
                message: "请输入门店",
                trigger: "blur",
              },
            ],
            // props: {
            //   label: 'rpsName',
            //   value: 'rpsNo',
            //   desc:'rpsNo'
            // },
            // // dicUrl: `/api/rb-oem/oem/posStore/pos_store/list?current=-1&size=-1`,
            // // dicFormatter:(data)=>{
            // //   return data.data.records
            // // },
            // dicData: [],
            // // change: (value) => {
            // //   if (value) {
            // //     this.search.rpstNo = '';
            // //     this.rpstNoList = [];
            // //     if (value.value) {
            // //       this.apiRpstList(value.value);//收银员
            // //     }
            // //   }
            // // },
            overHidden: true,
          },
          {
            label: "收银员",
            prop: "rpstName",
            type: "input",
            display: false,
            overHidden: true,
          },
          {
            label: "收银员",
            prop: "rpstNo",
            type: "select",
            sortable: true,
            filterable: true,
            search: true,
            searchOrder: 6,
            searchLabelWidth: 94,
            hide: true,
            display: false,
            rules: [
              {
                required: true,
                message: "请输入收银员",
                trigger: "blur",
              },
            ],
            // props: {
            //   label: 'rpstName',
            //   value: 'rpstNo',
            //   desc:'rpstNo'
            // },
            // // dicUrl: `/api/rb-oem/oem/posStaff/pos_staff/list?current=-1&size=-1`,
            // // dicFormatter:(data)=>{
            // //   return data.data.records
            // // },
            // dicData: [],

            overHidden: true,
          },
          {
            label: "类型",
            prop: "rptName",
            type: "input",
            // sortable: true,
            display: false,
            overHidden: true,
            minWidth: 140,
          },
          {
            label: "类型",
            prop: "rptNo",
            type: "select",
            sortable: true,
            search: true,
            searchOrder: 1,
            filterable: true,
            hide: true,
            display: false,
            rules: [
              {
                required: true,
                message: "请输入类型",
                trigger: "blur",
              },
            ],
            props: {
              label: "rptName",
              value: "rptNo",
              desc: "rptNo",
            },
            dicData: [],
            change: (value) => {
              if (value) {
                // 获取型号
                // this.search.rpsNo = '';
                // this.rpsNoList = [];
                this.search.rpmNo = "";
                this.rpmNoList = [];
                if (value.value) {
                  this.apiModelListList(value.value); //型号
                }
              }
            },
            overHidden: true,
          },
          {
            label: "型号",
            prop: "rpmName",
            type: "input",
            display: false,
            minWidth: 150,
            overHidden: true,
          },
          {
            label: "型号",
            prop: "rpmNo",
            type: "select",
            search: true,
            filterable: true,
            searchOrder: 3,
            hide: true,
            display: false,
            rules: [
              {
                required: true,
                message: "请输入型号",
                trigger: "blur",
              },
            ],
            props: {
              label: "rpmName",
              value: "rpmNo",
              desc: "rpmNo",
            },
            // dicUrl: `/api/rb-oem/oem/terminal/terminal/modelList?current=-1&size=-1`,
            // dicFormatter:(data)=>{
            //   return data.data.records
            // },
            dicData: [],
            minWidth: 150,
            overHidden: true,
          },

          {
            label: "终端状态",
            prop: "isOn",
            type: "select",
            dicUrl: "/api/blade-system/dict/dictionary?code=is_on",
            props: {
              label: "dictValue",
              value: "dictKey",
            },
            display: false,
            rules: [
              {
                required: true,
                message: "请选择终端状态",
                trigger: "blur",
              },
            ],
            overHidden: true,
          },
          {
            label: "绑定时间",
            prop: "createTime",
            type: "input",
            minWidth: 150,
            display: false,
            overHidden: true,
          },
        ],
      },
      data: [],
      excelForm: {},
      excelOption: {
        submitBtn: false,
        emptyBtn: false,
        column: [
          {
            label: "模板上传",
            prop: "excelFile",
            type: "upload",
            drag: true,
            loadText: "模板上传中，请稍等",
            span: 24,
            propsHttp: {
              res: "data",
            },
            tip: "请上传 .xls,.xlsx 标准格式文件",
            action: "/api/rb-core/terminal/terminal/import",
          },
          {
            label: "模板下载",
            prop: "excelTemplate",
            formslot: true,
            span: 24,
          },
        ],
      },
      terminalDialog: {
        disabled: false,
        visible: false,
        type: "add",
        text: "新建",
      }, //表格右侧操作add,edit,view
      terminalDForm: {}, //终端表单
      rptNoList: [], //类型
      rpsNoList: [], //供应商名称
      rpmNoList: [], //型号
      storeNoList: [], //门店
      rpstNoList: [], //收银员
      serviceNoList: [], //服务商
      hasPayPwd: false,
      twoPwdShow: false,
      towPwdForm: {
        towPwd: "",
        ids: "",
        rprNo: "",
        rsSn: "",
        rptNo: "",
        rpiNo: "",
      },
    };
  },
  computed: {
    ...mapGetters(["permission"]),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.terminal_add, false),
        viewBtn: this.vaildData(this.permission.terminal_view, false),
        delBtn: this.vaildData(this.permission.terminal_delete, false),
        editBtn: this.vaildData(this.permission.terminal_edit, false),
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach((ele) => {
        ids.push(ele.id);
      });
      return ids.join(",");
    },
  },
  watch: {
    // 'search.rptNo'(newVal, oldVal) {
    //     if (newVal !== oldVal) {
    //       this.search.rpsNo = '';
    //       const rpsNo = this.findObject(this.option.column, "rpsNo");
    //       rpsNo.dicData = [];
    //       this.search.rpmNo = '';
    //       const rpmNo = this.findObject(this.option.column, "rpmNo");
    //       rpmNo.dicData = [];
    //     }
    //   },
  },
  methods: {
    /** 列表--查询商户*/
    searchrpiNoChange(value){
      if (value) {
        this.search.storeNo = "";
        this.search.rpstNo = "";

        let params = {
          rpiNo: value
        };
        this.$refs['searchRpsSelect'].getOptionList(params);
        this.$refs['searchRpstSelect'].getOptionList(params);
      }
    },
    /** 列表--查询商户*/
    searchrpiNoClear(){
      this.search.storeNo = "";
      this.search.rpstNo = "";

      this.$refs['searchRpsSelect'].getOptionList();
      this.$refs['searchRpstSelect'].getOptionList();
    },
    /** 列表--查询门店*/
    searchrpsNoChange(value){
      if (value) {
        this.search.rpstNo = "";

        let params = {
          rpsNo: value,
          rpiNo:this.search.rpiNo
        };
        this.$refs['searchRpstSelect'].getOptionList(params);
      }
    },
    /** 列表--查询门店*/
    searchrpsNoClear(){
      this.search.rpstNo = "";

      this.$refs['searchRpstSelect'].getOptionList();
    },
    // 二级密码 关闭回调
    handleCloseTwoPwd(done) {
      this.towPwdForm.towPwd = "";
      done();
    },
    // 二级密码 确定
    twoPwdSubmit() {
      let that = this;
      this.$refs.towPwdForm.validate((valid) => {
        if (valid) {
          if (that.towPwdForm.rptNo == 18) {
            // 第三方
            remove(that.towPwdForm.ids, md5(that.towPwdForm.towPwd + "")).then(
              (res) => {
                that.onLoad(this.page);
                that.$message({
                  type: "success",
                  message: res.data.msg,
                });
                that.$refs.crud.toggleSelection();
                that.twoPwdShow = false;
                that.towPwdForm.towPwd = "";
              }
            );
          } else if (that.towPwdForm.rptNo == 20) {
            // 传统pos
            let param = {
              rsSn: that.towPwdForm.rprNo,
              rpiNo: that.towPwdForm.rpiNo,
              payPwd: md5(that.towPwdForm.towPwd + ""),
            };
            unBindYSPOS(param).then((res) => {
              that.onLoad(this.page);
              that.$message({
                type: "success",
                message: res.data.msg,
              });
              that.$refs.crud.toggleSelection();
              that.twoPwdShow = false;
              that.towPwdForm.towPwd = "";
            });
          } else if (that.towPwdForm.rptNo == 24) {
            // 智能机
            let param = {
              rsSn: that.towPwdForm.rprNo,
              rpiNo: that.towPwdForm.rpiNo,
              payPwd: md5(that.towPwdForm.towPwd + ""),
            };
            unBindZNJ(param).then((res) => {
              that.onLoad(this.page);
              that.$message({
                type: "success",
                message: res.data.msg,
              });
              that.$refs.crud.toggleSelection();
              that.twoPwdShow = false;
              that.towPwdForm.towPwd = "";
            });
          } else if (that.towPwdForm.rptNo == 23) {
            // 扫码设备
            let param = {
              rsSn: that.towPwdForm.rprNo,
              rpiNo: that.towPwdForm.rpiNo,
              payPwd: md5(that.towPwdForm.towPwd + ""),
            };
            removeSM(param).then((res) => {
              that.onLoad(this.page);
              that.$message({
                type: "success",
                message: res.data.msg,
              });
              that.$refs.crud.toggleSelection();
              that.twoPwdShow = false;
              that.towPwdForm.towPwd = "";
            });
          } else {
            let param = {
              payPwd: md5(that.towPwdForm.towPwd + ""),
              rpiNo: that.towPwdForm.rpiNo,
              rprNo: that.towPwdForm.rprNo,
            };

            terRemove(param).then((res) => {
              that.onLoad(this.page);
              that.$message({
                type: "success",
                message: res.data.msg,
              });
              that.$refs.crud.toggleSelection();
              that.twoPwdShow = false;
              that.towPwdForm.towPwd = "";
            });
          }
        } else {
          return false;
        }
      });
    },
    // 是否 有二级密码
    getUserHasInfo() {
      userHasInfo().then(
        (res) => {
          this.hasPayPwd = res.data.data.hasPayPwd;
        },
        (error) => {
          window.console.log(error);
        }
      );
    },
    // 跳转用户信息页面
    toUserInfo() {
      this.$confirm("请您先设置二级密码，再操作！", "提示", {
        confirmButtonText: "确定",
        showCancelButton: false,
        type: "warning",
      }).then(() => {
        this.$router.push({ path: "/info/index", query: { index: 2 } });
      });
    },
    // 跳转 商户详情终端管理
    goto(row) {
      let terminalIndex = 0;

      // if(row.rpmNo == 'R00001'){
      //   terminalIndex = 0;
      // }else if(row.rpmNo == 'A00001'){
      //   terminalIndex = 2;
      // }
      if (row.rptNo == "15") {
        //台卡
        terminalIndex = 0;
      } else if (row.rptNo == "18") {
        //第三方
        terminalIndex = 2;
      } else if (row.rptNo == "23") {
        //扫码设备
        terminalIndex = 3;
      } else if (row.rptNo == "20") {
        //传统POS
        terminalIndex = 4;
      } else if (row.rptNo == "32") {
        //收款单
        terminalIndex = 6;
      }
      

      this.$router.push({
        name: "终端管理",
        query: {
          rpiNo: row.rpiNo,
          terminalIndex,
        },
      });
    },
    //查看二维码
    getqrCodeUrl(item) {
      if (item.rpmNo == "R00001"||item.rptNo == '32') {
        this.rqUrlImage[0] = item.qrcodeUrl;
        this.payVoucherDialog = true;
      }
    },
    // 新建
    handleSave() {
      this.terminalDialog.visible = true;
      this.terminalDialog.type = "add";
      this.terminalDialog.text = "新建终端";
      this.terminalDialog.disabled = false;
    },
    // 编辑
    handleEdit(row) {
      this.terminalDialog.visible = true;
      this.terminalDialog.type = "edit";
      this.terminalDialog.text = "修改终端";
      this.terminalDialog.disabled = false;
      this.terminalDForm = JSON.parse(JSON.stringify(row));
    },
    handleView(row) {
      this.terminalDialog.visible = true;
      this.terminalDialog.type = "view";
      this.terminalDialog.text = "查看";
      this.terminalDialog.disabled = true;
      this.terminalDForm = JSON.parse(JSON.stringify(row));
    },
    /**
     * @func 终端状态开关
     * @desc 终端状态开关 0否 1是
     * @param {Object} row - 终端信息
     * */
    handleSwitch(row) {
      incomingTerminalOn(row.id, row.isOn).then(
        () => {},
        (error) => {
          row.isOn = row.isOn == 1 ? 0 : 1;
          window.console.log(error);
        }
      );
    },
    // 保存
    rowSave(row, done, loading) {
      add(row).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          done();
        },
        (error) => {
          loading();
          window.console.log(error);
        }
      );
    },
    rowUpdate(row, index, done, loading) {
      update(row).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          done();
        },
        (error) => {
          loading();
          window.console.log(error);
        }
      );
    },
    rowDel(row) {
      if (!this.hasPayPwd) {
        this.toUserInfo();
      } else {
        this.towPwdForm.ids = row.id;
        this.towPwdForm.rptNo = row.rptNo; //设备类型编号
        (this.towPwdForm.rpiNo = row.rpiNo), //商户编号
          (this.towPwdForm.rprNo = row.rsSn);

        this.twoPwdShow = true;
      }
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      if (!this.hasPayPwd) {
        this.toUserInfo();
      } else {
        this.towPwdForm.ids = this.ids;
        this.twoPwdShow = true;
      }
    },
    beforeOpen(done, type) {
      if (["edit", "view"].includes(type)) {
        getDetail(this.form.id).then((res) => {
          this.form = res.data.data;
        });
      }
      done();
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getList(
        page.currentPage,
        page.pageSize,
        Object.assign(params, this.query, { eqType: 1 })
      ).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
    },
    handleImport() {
      this.excelBox = true;
    },
    handleTemplate() {
      window.open(
        `/api/rb-oem/oem/terminal/terminal/export-template?${
          this.website.tokenHeader
        }=${getToken()}`
      );
    },
    uploadAfter(res, done) {
      this.excelBox = false;
      this.refreshChange();
      done();
    },
    sortChange(val) {
      if (val.order == "ascending") {
        //正序
        this.query.ascs = val.prop;
        this.query.descs = "";
      } else {
        //倒序
        this.query.descs = val.prop;
        this.query.ascs = "";
      }
      this.refreshChange();
    },
    // 终端---新建弹框 关闭前回调
    terminalDialogHandleClose(done) {
      this.$refs["terminalDialog"].initEmpty();
      done();
    },
    clickCopy(ht) {
      const save = function (e) {
        e.clipboardData.setData("text/plain", ht);
        e.preventDefault(); // 阻止默认行为
      };
      const once = {
        once: true,
      };
      document.addEventListener("copy", save, once); // 添加一个copy事件
      document.execCommand("copy"); // 执行copy方法
      this.$message({ message: "复制成功", type: "success" });
    },
    // 终端---新建弹框提交
    terminalDialogSubmit() {
      /**
       * add  新建
       * edit 修改
       * view 查看详情
       */
      let that = this;
      if (this.terminalDialog.type !== "view") {
        this.$refs["terminalDialog"].$refs["terminalDialogForm"].validate(
          (valid) => {
            if (valid) {
              if (
                this.$refs["terminalDialog"].terminalDialogForm.rpmNo ==
                "A00001"
              ) {
                this.$refs["terminalDialog"].terminalDialogForm.rsSn = "";
              }

              let param = {
                ...this.$refs["terminalDialog"].terminalDialogForm,
              };
              if (this.terminalDialog.type == "add") {
                if (param.rpmNo == "A00001") {
                  add(param).then(
                    (res) => {
                      const h = this.$createElement;
                      this.$msgbox({
                        title: "",
                        message: h("div", null, [
                          h(
                            "h4",
                            { style: "margin:0 0 10px 0;color:#f00;" },
                            res.data.msg
                          ),
                          h("div", null, `商户号：${param.rpiNo}`),
                          h("div", null, `终端号：${res.data.data.appkey}`),
                          h(
                            "div",
                            null,
                            `密钥(token)：${res.data.data.secret}`
                          ),
                        ]),
                        showCancelButton: true,
                        confirmButtonText: "确定",
                        cancelButtonText: "复制",
                        showClose: false,
                        beforeClose: (action, instance, done) => {
                          if (action === "confirm") {
                            done();
                          } else {
                            let text = `商户号：${param.rpiNo},终端号：${res.data.data.appkey},密钥(token)：${res.data.data.secret}`;
                            that.clickCopy(text);
                          }
                        },
                      }).then(() => {
                        that.onLoad(this.page);
                        that.$refs["terminalDialog"].initEmpty();

                        that.$message({
                          type: "success",
                          message: "操作成功!",
                        });
                        that.terminalDialog.visible = false;
                      });
                    },
                    (error) => {
                      window.console.log(error);
                    }
                  );
                } else {
                  add(param).then(
                    () => {
                      that.onLoad(this.page);
                      that.$refs["terminalDialog"].initEmpty();

                      that.$message({
                        type: "success",
                        message: "操作成功!",
                      });
                      that.terminalDialog.visible = false;
                    },
                    (error) => {
                      window.console.log(error);
                    }
                  );
                }
              } else if (this.terminalDialog.type == "edit") {
                update(param).then(
                  () => {
                    this.onLoad(this.page);
                    this.$refs["terminalDialog"].initEmpty();

                    this.$message({
                      type: "success",
                      message: "操作成功!",
                    });
                    this.terminalDialog.visible = false;
                  },
                  (error) => {
                    window.console.log(error);
                  }
                );
              }
            }
          }
        );
      }
    },
    // 获取 类型名称
    requestRptNo() {
      getRptNo(-1, -1, { eqType: 1 }).then((res) => {
        let rptNoList = JSON.parse(JSON.stringify(res.data.data.records));
        res.data.data.records.forEach(element => {
          if(element.rptNo !='32'){
            this.rptNoList.push(element)
          }
         })

        const column = this.findObject(this.option.column, "rptNo");
        column.dicData = rptNoList;
        this.$forceUpdate();
      });
    },
    // 获取型号
    apiModelListList(rptNo = "") {
      let param = {};
      if (rptNo) {
        param.rptNo = rptNo;
      }
      getModelList(-1, -1, param).then((res) => {
        this.rpmNoList = res.data.data.records;
        const column = this.findObject(this.option.column, "rpmNo");
        column.dicData = this.rpmNoList;
        this.$forceUpdate();
      });
    },
    // 获取 商户
    // requestrpiNo() {
    //   getrpiNo(-1, -1).then((res) => {
    //     this.rpiNoList = res.data.data.records;
    //     const column = this.findObject(this.option.column, "rpiNo");
    //     column.dicData = this.rpiNoList;
    //     this.$forceUpdate();
    //   });
    // },
    // 获取 门店名称
    // rpiNoListChange(rpiNo = "") {
    //   let param = {};
    //   if (rpiNo) {
    //     param.rpiNo = rpiNo;
    //   }
    //   getstoreNo(-1, -1, param).then((res) => {
    //     this.storeNoList = res.data.data.records;
    //     const column = this.findObject(this.option.column, "storeNo");
    //     column.dicData = this.storeNoList;
    //     this.$forceUpdate();
    //   });
    // },
    // // 获取 收银员名称
    // apiRpstList(rpsNo = "") {
    //   let param = {};
    //   if (rpsNo) {
    //     param.rpsNo = rpsNo;
    //   }
    //   getrpstNo(-1, -1, param).then((res) => {
    //     this.rpstNoList = res.data.data.records;
    //     const column = this.findObject(this.option.column, "rpstNo");
    //     column.dicData = this.rpstNoList;
    //     this.$forceUpdate();
    //   });
    // },
    initRequest() {
      this.requestRptNo(); //获取 类型
      this.apiModelListList(); //型号

      // this.requestrpiNo();//获取 商户
      // this.rpiNoListChange();//门店
      // this.apiRpstList();//收银员名称

      this.getUserHasInfo(); // 是否是设置二级密码
      this.$forceUpdate();
    },
  },
  created() {
    this.initRequest();
  },
};
</script>


<style lang="scss" scoped>
.rsSnblue {
  color: #409eff;
  cursor: pointer;
}
</style>
